@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

REM ========================================
REM Database System Test Script
REM Function: Start server and client, support automatic SQL script execution
REM ========================================

echo [INFO] Database system test script started
echo [INFO] Current time: %date% %time%
echo.

REM Set path variables (support paths with spaces)
set "SERVER_DIR=%~dp0dbms-server\DBMS_Server"
set "CLIENT_DIR=%~dp0dbms-client\DBMS_Client"

REM Check for server executable (prioritize x64 Debug as specified by user)
set "SERVER_BASE=%~dp0dbms-server"
if exist "%SERVER_BASE%\x64\Debug\DBMS_Server.exe" (
    set "SERVER_EXE=%SERVER_BASE%\x64\Debug\DBMS_Server.exe"
) else if exist "%SERVER_BASE%\x64\Release\DBMS_Server.exe" (
    set "SERVER_EXE=%SERVER_BASE%\x64\Release\DBMS_Server.exe"
) else if exist "%SERVER_DIR%\Debug\DBMS_Server.exe" (
    set "SERVER_EXE=%SERVER_DIR%\Debug\DBMS_Server.exe"
) else if exist "%SERVER_DIR%\Release\DBMS_Server.exe" (
    set "SERVER_EXE=%SERVER_DIR%\Release\DBMS_Server.exe"
) else (
    set "SERVER_EXE=%SERVER_DIR%\DBMS_Server.exe"
)

REM Check for client executable (prioritize x64 Debug as specified by user)
set "CLIENT_BASE=%~dp0dbms-client"
if exist "%CLIENT_BASE%\x64\Debug\DBMS_Client.exe" (
    set "CLIENT_EXE=%CLIENT_BASE%\x64\Debug\DBMS_Client.exe"
) else if exist "%CLIENT_BASE%\x64\Release\DBMS_Client.exe" (
    set "CLIENT_EXE=%CLIENT_BASE%\x64\Release\DBMS_Client.exe"
) else if exist "%CLIENT_DIR%\Debug\DBMS_Client.exe" (
    set "CLIENT_EXE=%CLIENT_DIR%\Debug\DBMS_Client.exe"
) else if exist "%CLIENT_DIR%\Release\DBMS_Client.exe" (
    set "CLIENT_EXE=%CLIENT_DIR%\Release\DBMS_Client.exe"
) else (
    set "CLIENT_EXE=%CLIENT_DIR%\DBMS_Client.exe"
)
set "SQL_SCRIPT=%~dp0test_script.sql"
set "AUTO_SQL_INPUT=%~dp0auto_sql_input.txt"
set "OUTPUT_FILE=%~dp0auto_sql_output.txt"
set "TEMP_SCRIPT=%~dp0temp_auto_script.bat"

REM Check if files exist
echo [INFO] Checking necessary files...
if not exist "%SERVER_EXE%" (
    echo [ERROR] Server executable not found: %SERVER_EXE%
    pause
    exit /b 1
)

if not exist "%CLIENT_EXE%" (
    echo [ERROR] Client executable not found: %CLIENT_EXE%
    pause
    exit /b 1
)

echo [INFO] Server path: %SERVER_EXE%
echo [INFO] Client path: %CLIENT_EXE%
echo.

REM Check SQL script files
set "SCRIPT_TO_USE="
if exist "%SQL_SCRIPT%" (
    set "SCRIPT_TO_USE=%SQL_SCRIPT%"
    echo [INFO] Found SQL script file: %SQL_SCRIPT%
) else if exist "%AUTO_SQL_INPUT%" (
    set "SCRIPT_TO_USE=%AUTO_SQL_INPUT%"
    echo [INFO] Found SQL script file: %AUTO_SQL_INPUT%
) else (
    echo [WARNING] No SQL script file found (test_script.sql or auto_sql_input.txt)
    echo [WARNING] Will start client in interactive mode
)

echo.

REM Start server
echo [INFO] Starting database server...
start "Database Server" cmd /k "chcp 65001 && cd /d "%SERVER_DIR%" && "%SERVER_EXE%""

REM Wait for server to start
echo [INFO] Waiting for server to start...
echo [INFO] Server will start in 5 seconds...
timeout /t 5 /nobreak >nul

:CLIENT_START
REM Start client
echo [INFO] Starting database client...

if defined SCRIPT_TO_USE (
    echo [INFO] Script mode: Will automatically execute SQL script
    echo [INFO] Script file: !SCRIPT_TO_USE!
    echo [INFO] Output file: %OUTPUT_FILE%
    
    REM Create temporary script to handle client startup and output redirection
    echo @echo off > "%TEMP_SCRIPT%"
    echo chcp 65001 >> "%TEMP_SCRIPT%"
    echo cd /d "%CLIENT_DIR%" >> "%TEMP_SCRIPT%"
    echo echo [INFO] Client starting... >> "%TEMP_SCRIPT%"
    echo echo [INFO] Executing SQL script: !SCRIPT_TO_USE! >> "%TEMP_SCRIPT%"
    echo echo [INFO] Please wait for execution to complete... >> "%TEMP_SCRIPT%"
    echo echo. >> "%TEMP_SCRIPT%"
    echo "%CLIENT_EXE%" --script "!SCRIPT_TO_USE!" >> "%TEMP_SCRIPT%"
    echo echo. >> "%TEMP_SCRIPT%"
    echo echo [INFO] Script execution completed >> "%TEMP_SCRIPT%"
    echo echo [INFO] Press any key to continue... >> "%TEMP_SCRIPT%"
    echo pause >> "%TEMP_SCRIPT%"
    
    start "Database Client - Script Mode" cmd /k "%TEMP_SCRIPT%"
) else (
    echo [INFO] Interactive mode: Manually enter username and password
    start "Database Client - Interactive Mode" cmd /k "chcp 65001 && cd /d \"%CLIENT_DIR%\" && \"%CLIENT_EXE%\""
)

echo.
echo [INFO] Client started
echo.
echo ========================================
echo Operation options:
echo 1. Restart client (keep server running)
echo 2. Exit script
echo ========================================
echo.

set /p choice="Please select operation (1 or 2): "

if "%choice%"=="1" (
    echo [INFO] Restarting client...
    echo.
    goto CLIENT_START
) else if "%choice%"=="2" (
    echo [INFO] Exiting script
    goto END
) else (
    echo [WARNING] Invalid selection, exiting script
    goto END
)

:END
REM Clean up temporary files
if exist "%TEMP_SCRIPT%" (
    del "%TEMP_SCRIPT%" >nul 2>&1
    echo [INFO] Temporary file cleanup completed
)

echo [INFO] Script execution completed
echo [INFO] Note: Server is still running, please manually close server window
echo.
pause
endlocal